package com.szh.es.service;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szh.es.mapper.HotelMapper;
import com.szh.es.pojo.HotelDoc2;
import com.szh.es.pojo.HotelEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
public class HotelServiceImpl extends ServiceImpl<HotelMapper, HotelEntity> implements HotelService  {
    @Resource
    RestHighLevelClient esClient;

    @Override
    public void imporeES() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        List<HotelEntity> page = this.list( );

        System.err.println(page.size());
        page.forEach(entity ->{
            bulkRequest.add(new IndexRequest("hotel2")
                    .id(entity.getId().toString())
                    .source(JSONUtil.toJsonStr(new HotelDoc2(entity)), XContentType.JSON));
        });


        BulkResponse bulkResponse = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.getTook());
        System.out.println(bulkResponse.getItems().length);

    }
}
